package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketComment;
import com.cskaoyan.market.db.domain.MarketCommentExample;
import com.cskaoyan.market.db.mapper.MarketCommentMapper;
import com.cskaoyan.market.service.AdminCommentService;
import com.cskaoyan.market.util.MybatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class AdminCommentServiceImpl implements AdminCommentService {
    @Override
    public List<MarketComment> list(String page, String limit, String userId, String valueId, String sort, String order) {
        // 获取session
        SqlSession session = MybatisUtil.getSession();
        MarketCommentMapper mapper = session.getMapper(MarketCommentMapper.class);
        MarketCommentExample commentExample = new MarketCommentExample();
        MarketCommentExample.Criteria or = commentExample.or();
        // 设置判断逻辑删除
        or.andLogicalDeleted(true);
        // 通过用户id查询
        if (!StringUtils.isEmpty(userId)) {
            or.andUserIdEqualTo(Integer.parseInt(userId));
        }
        // 通过商品id查询
        if (!StringUtils.isEmpty(valueId)) {
            or.andValueIdEqualTo(Integer.parseInt(valueId));
        }
        // 设置排序
        commentExample.setOrderByClause(sort + " " + order);
        // 设置分页
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<MarketComment> marketComments = mapper.selectByExample(commentExample);
        // 提交关闭事务
        session.commit();
        session.close();

        return marketComments;
    }

    @Override
    public void delete(String id) {
        SqlSession session = MybatisUtil.getSession();
        MarketCommentMapper mapper = session.getMapper(MarketCommentMapper.class);
        mapper.logicalDeleteByPrimaryKey(Integer.parseInt(id));
        session.commit();
        session.close();
    }
}
